/**
 * author: cwl
 */
;(function (global, factory) {
  global.Pen = factory();
}(this, (function () {
  'use strict'
  var defaultPen = {
    penColor: '#000',
    penWidth: '2'
  };
  var canvasColor = '#fff';
  var currentPenColor = '';
  var currentPenWidth = '';
  var Pen = function (canvas) {
    init(this, canvas);
  }
  var fn = Pen.prototype;

  fn.reLoad = function () { //重载画笔
    this.context.strokeStyle = this.penColor;
    this.context.lineWidth = this.penWidth;
    this.canvas.style.backgroundColor = this.canvasColor;
  }
  fn.setPenColor = function (color) { //设置画笔颜色
    this.penColor = color;
    currentPenColor = color;
    this.reLoad();
  }
  fn.setPenWidth = function (width) { //设置画笔粗细
    this.penWidth = width;
    currentPenWidth = width;
    this.reLoad();
  }
  fn.setCanvasColor = function (color) {//设置画布颜色
    this.canvasColor = color;
    this.reLoad();
  }
  fn.startPoint = function (x, y) { //设置开始点
    this.context.beginPath();
    this.context.moveTo(x, y);
  }
  fn.drawTo = function (x, y) { //画直线
    this.context.lineTo(x, y);
    this.context.stroke();
    console.log(x,y);
  }

  fn.clearCanvas = function () { //清空画布
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }
  fn.clearLine = function () {//橡皮檫
    this.penColor = canvasColor;
    this.reLoad();
  }
  fn.defaultPen = function () {//清除画笔样式
    this.penColor = defaultPen.penColor;
    this.penWidth = defaultPen.penWidth;
    this.reLoad();
  }
  fn.currentPen = function () {
    this.penColor = currentPenColor;
    this.penWidth = currentPenWidth;
    this.reLoad();
  }
  fn.setPenIcon = function (str) { //设置画笔图案，传入可选参数 pen|eraser
    this.canvas.className = str;
  }


  //初始化画笔
  function init(context, canvas) {
    context.canvas = document.querySelector(canvas);
    context.context = context.canvas.getContext('2d');
    context.penColor = defaultPen.penColor;
    context.penWidth = defaultPen.penWidth;
    currentPenColor = defaultPen.penColor;
    currentPenWidth = defaultPen.penWidth;
    context.canvasColor = canvasColor;
    context.reLoad();
  }

  return Pen;
})));